﻿using PLCS.App.Shared.Events;
using PLCS.App.Shared.Extensions;
using PLCS.App.Shared.Service;
using PLCS.App.Shared.Service.Dialog;
using PLCS.Application.Contracts.EquipmentApp.Dtos;
using PLCS.Application.Contracts.StationApp.Dtos;
using PLCS.Client.IService.EquipmentManager;

namespace PLCS.App.ViewModels;

public class MonitorViewModel : BindableBase
{
    private readonly IHostDialogService _dialog;
    private readonly IStationService _stationService;
    private readonly IEquipmentService _equipmentService;
    private readonly CacheSettingService _cacheSettingService;
    private readonly IEventAggregator _aggregator;

    public DelegateCommand<string> OpenDetailCommand { get; set; }
    public DelegateCommand OpenErrorDialogCommand { get; set; }
    public DelegateCommand ChangeTaskRunningCommand { get; }
    public DelegateCommand SendTaskCommand { get; }

    private EquipmentDto _equipmentModel;

    public EquipmentDto EquipmentModel
    {
        get { return _equipmentModel; }
        set { SetProperty(ref _equipmentModel, value); }
    }

    private StationDto _stationModel;

    public StationDto StationModel
    {
        get { return _stationModel; }
        set { SetProperty(ref _stationModel, value); }
    }

    private string _trayNo;

    public string TrayNo
    {
        get { return _trayNo; }
        set { SetProperty(ref _trayNo, value); }
    }

    private int _isTaskRunning;

    public int IsTaskRunning
    {
        get { return _isTaskRunning; }
        set { SetProperty(ref _isTaskRunning, value); }
    }

    public int DisplayMode { get; set; }

    public MonitorViewModel(IHostDialogService dialog, IStationService stationService, IEquipmentService equipmentService, CacheSettingService cacheSettingService, IEventAggregator aggregator)
    {
        OpenDetailCommand = new DelegateCommand<string>(OpenDetail);
        SendTaskCommand = new DelegateCommand(SendTask);
        ChangeTaskRunningCommand = new DelegateCommand(ChangeTaskRunning);
        OpenErrorDialogCommand = new DelegateCommand(OpenErrorDialog);
        _dialog = dialog;
        _stationService = stationService;
        _equipmentService = equipmentService;
        _cacheSettingService = cacheSettingService;
        _aggregator = aggregator;
        DisplayMode = _cacheSettingService.cacheSetting.DisplayMode;
        IsTaskRunning = _cacheSettingService.cacheSetting.IsTaskRunning;
    }

    private async void OpenErrorDialog()
    {
        await _dialog.ShowDialogAsync("ErrorDialogView");
    }

    private async void ChangeTaskRunning()
    {
        IsTaskRunning = (IsTaskRunning == 0) ? 1 : 0;
        var model = _cacheSettingService.cacheSetting;
        model.IsTaskRunning = IsTaskRunning;
        await _cacheSettingService.UpdateCacheSetting(model);
    }

    private async void SendTask()
    {
        if (StationModel == null)
            return;
        DialogParameters param = new DialogParameters()
        {
            {"Value", StationModel}
        };
        await _dialog.ShowDialogAsync("SendTaskDialogView", param);
    }

    private async void OpenDetail(string obj)
    {
        string type = obj.Split('-')[0];
        string name = obj.Split("-")[1];

        try
        {
            switch (type)
            {
                case "RGV" or "Stacker":
                    var equipment = await _equipmentService.GetByEquipmentNoAsync(name);
                    var containStation = await _stationService.GetByStationNoAsync(name);

                    if (DisplayMode == 0)
                    {
                        var equipmentparam = new DialogParameters
                        {
                            { "Value", equipment },
                            { "Station", containStation }
                        };
                        var equipmentDialogResult = await _dialog.ShowDialogAsync("EquipmentInfoView", equipmentparam);
                    }
                    else if (DisplayMode == 1)
                    {
                        EquipmentModel = equipment;
                        StationModel = containStation;
                    }

                    break;

                case "Station" or "RotaryTable" or "ConveyorLine" or "Pipeline":
                    var station = await _stationService.GetByStationNoAsync(name);
                    if (DisplayMode == 0)
                    {
                        var stationparam = new DialogParameters
                         {
                    {    "Value", station }
                         };
                        var stationDialogResult = await _dialog.ShowDialogAsync("StationInfoView", stationparam);
                    }
                    else if (DisplayMode == 1)
                    {
                        try
                        {
                            EquipmentModel = await _equipmentService.GetByEquipmentNoAsync(name);
                        }
                        catch (Exception ex)
                        {
                        }

                        StationModel = station;
                        TrayNo = station.Trays.FirstOrDefault()?.TrayNo;
                    }

                    break;

                case "RowGoods":
                    var parameters = name.Split('，');
                    var stations = await _stationService.GetBySNameFuzzyAsync($"{parameters[0]}-{parameters[1]}-");
                    var statioDialognparam = new DialogParameters
                         {
                            {    "Models", stations },
                            {  "ColumnCount",parameters[2] },
                            {  "LayerCount",parameters[3] },
                            {  "Platepos",parameters[4] },
                         };
                    var rowDialogResult = await _dialog.ShowDialogAsync("RowGoodsDetailView", statioDialognparam);
                    break;
            }
        }
        catch (Exception ex)
        {
        }
    }

    public void UpdateLoading(bool IsOpen)
    {
        _aggregator.UpdateLoading(new UpdateModel()
        {
            IsOpen = IsOpen
        });
    }
}